package com.newenergy.wisehome.ui.fragment;


import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.data.bean.BallLight;
import com.newenergy.wisehome.data.bean.DeviceCategory;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.ormlite.dao.BallLightDao;
import com.newenergy.wisehome.data.ormlite.dao.DeviceCategoryDao;
import com.newenergy.wisehome.data.ormlite.dao.FamilyDao;
import com.newenergy.wisehome.data.ormlite.dao.IconDao;
import com.newenergy.wisehome.gateway.manager.BLLocalFamilyManager;
import com.newenergy.wisehome.ui.adapter.IconAdapter;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;


/**
 * A simple {@link Fragment} subclass.
 */
public class CreateGroupFramgment extends BaseFragment {


    private static String ARG_CREATEGROUP = "arg_creategroup";
    @BindView(R.id.btn_back)
    Button mBtnBack;
    @BindView(R.id.tv_back)
    TextView mTvBack;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    @BindView(R.id.btn_add)
    Button mBtnAdd;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.btn_select_icon)
    Button mBtnSelectIcon;
    @BindView(R.id.btn_confirm)
    Button mBtnConfirm;
    @BindView(R.id.btn_cancel)
    Button mBtnCancel;
    private EasyRecyclerView mRecyclerView;
    private IconAdapter mIconAdapter;
    //    private Icon mIcon;
    private PopupWindow mPopupWindow;
    private IconDao mIconDao;
    private Family mCurrFamily;

    public CreateGroupFramgment() {

    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_create_group;
    }


    public static ISupportFragment newInstance(Family currFamily) {
        Bundle args = new Bundle();
        args.putParcelable(ARG_CREATEGROUP, currFamily);
        CreateGroupFramgment fragment = new CreateGroupFramgment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void init(View rootView) {
        mTvPage.setText(getString(R.string.create_new_room));
        mBtnSelectIcon.setVisibility(View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
        mCurrFamily = getArguments().getParcelable(ARG_CREATEGROUP);
    }


    @OnClick({R.id.btn_back, R.id.tv_back, R.id.btn_select_icon, R
            .id.btn_confirm, R.id.btn_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
            case R.id.btn_select_icon:
//                showPopupWindow();
                break;
            case R.id.btn_confirm:
                createGroup();
                break;
            case R.id.btn_cancel:
                doBack();
                break;
        }
    }


    private void createGroup() {
        try {
            String name = mEtName.getText().toString().trim();
            String pinyinString = PinyinHelper.convertToPinyinString(name, " ", PinyinFormat.WITHOUT_TONE);

            if (!checkGroupName(name)) return;
            CreateBallLight(name, pinyinString);


//            ToastUtil.myToast(getString(R.string.create) + name + getString(R.string.success), false);
            mEtName.setText("");


            doBack();
        } catch (PinyinException e) {
            e.printStackTrace();
        }

    }


    //创建球泡灯
    private void CreateBallLight(String name, String pinyinString) {
        FamilyDao familyDao = new FamilyDao(getContext());
        BallLightDao ballLightDao = new BallLightDao(getContext());
        DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(getContext());
        int count = mCurrFamily.getBallLightCount();
        count++;
        if (count > 255) count = 0;
        mCurrFamily.setBallLightCount(count);
        String groupId = intToHex(count);

        String scenedata = mCurrFamily.getFamilyId() + groupId + "FF000000" + "FFFFFF";

        BallLight group = new BallLight(mCurrFamily.getFamilyId(), 1, groupId, name, pinyinString, "btn_balllight", scenedata);

        familyDao.updateFamily(mCurrFamily);
        ballLightDao.add(group);

        DeviceCategory deviceCategory = group.covertToDeviceCategory();
        deviceCategoryDao.add(deviceCategory);
        BLLocalFamilyManager.getInstance().createOneBlDevice(mCurrFamily, deviceCategory);
    }

    public void showPopupWindow() {
        int width = mEtName.getWidth();
        mPopupWindow = new PopupWindow(mRecyclerView, width, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setOutsideTouchable(false);
        mPopupWindow.setFocusable(true);
        mPopupWindow.showAsDropDown(mEtName, 0, dpToPx(3));
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
